#include "MainScene.h"

static std::mutex MainSceneLocker;
static MainScene* _Scene = nullptr;

MainScene::MainScene()
{
	_Scene = this;
}

MainScene::~MainScene(){}

MainScene* MainScene::GetInstance()
{
	if (_Scene == nullptr)
	{
		ThreadLocker Locker(MainSceneLocker);
		if (_Scene == nullptr)
		{
			_Scene = new MainScene();
		}
	}
	return _Scene;
}

bool MainScene::init()
{
	bool bRef = false;
	do
	{
		//TypingPage = (TypingFrame*)InitPageByTag(TYPING_LAYER_TAG);
		MainPage = (HelloWorld*)InitPageByTag(MAIN_LAYER_TAG);
		SettingPage = (SettingsLayer*)InitPageByTag(SETING_LAYER_TAG);
		WaitForConnectPage = (WaitForConnectLayer*)InitPageByTag(WAIT_FOR_CONNECT_TAG);
		WaitForConnectPage->setVisible(false);
		//_Scene->addChild(TypingPage);
		_Scene->addChild(WaitForConnectPage);
		_Scene->addChild(MainPage);
		_Scene->addChild(SettingPage);
		bRef = true;
	} while (0);


	return bRef;
}

Layer* MainScene::InitPageByTag(uint16 Tag)
{
	switch (Tag)
	{
		case MAIN_LAYER_TAG:
		{
			HelloWorld* Temp = HelloWorld::create();
			Temp->setTag(Tag);
			return Temp;
		}
		case SETING_LAYER_TAG:
		{
			SettingsLayer* Temp = SettingsLayer::create();
			Temp->setTag(SETING_LAYER_TAG);
			Temp->setVisible(false);
			return Temp;
		}
		case TYPING_LAYER_TAG:
		{
			TypingFrame* Temp = TypingFrame::create();
			Temp->setTag(TYPING_LAYER_TAG);
			return Temp;
		}
		case WAIT_FOR_CONNECT_TAG:
		{
			WaitForConnectLayer* Temp = WaitForConnectLayer::create();
			Temp->setTag(WAIT_FOR_CONNECT_TAG);
			return Temp;
		}
		case CHOSE_CHARACTER_LAYER_TAG:
		{
			ChoseCharacterLayer* Temp = ChoseCharacterLayer::create();
			Temp->setTag(CHOSE_CHARACTER_LAYER_TAG);
			return Temp;
		}
	}
}

void MainScene::SwapLayerByTag(uint16 Changed, uint16 Instead)
{
	if (Layer* _Changed = (Layer*)getChildByTag(Changed))
	{
		_Changed->removeFromParentAndCleanup(true);
		addChild(InitPageByTag(Instead));
	}
}